package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    @Lazy
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    /**
     * @Description: 分页查询房间信息
     * @Param: [roomItemVoIPage, queryVo]
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.atguigu.lease.web.admin.vo.room.RoomItemVo>
     * @Author: ahang
     * @Date: 2025/7/29
     */

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> roomItemVoIPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(roomItemVoIPage, queryVo);

    }
    /**
     * @Description: 根据ID获取房间具体的信息
     * @Param: [id]
     * @return: com.atguigu.lease.web.admin.vo.room.RoomDetailVo
     * @Author: ahang
     * @Date: 2025/7/29
     */

    @Override
    public RoomDetailVo getDetailById(Long id) {
//        1. 获得房间基本信息
        RoomInfo roomInfo = super.getById(id);
//        2. 获取公寓信息
        LambdaQueryWrapper<ApartmentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApartmentInfo::getId, roomInfo.getApartmentId());
        ApartmentInfo apartmentInfo = apartmentInfoService.getOne(queryWrapper);
//        3.获取图片信息
        List<GraphVo> graphInfoList = graphInfoMapper.listByItemId(id, ItemType.ROOM);
//        4.获取属性值
        List<AttrValueVo> attrValueVoList = roomAttrValueMapper.listByItemId(id);
//        5. 配套信息
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.listByItemId(id);
//        6. 标签信息
        List<LabelInfo> labelInfoList = roomLabelMapper.listByItemId(id);
//        7.支付方式
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.listByItemId(id);
//        8.租期
        List <LeaseTerm> leaseTermList = roomLeaseTermMapper.listByItemId(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setGraphVoList(graphInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        return roomDetailVo;
    }

    /**
     * @Description: 保存或更新房间信息
     * @Param: [roomSubmitVo]
     * @return: void
     * @Author: ahang
     * @Date: 2025/7/29
     */

    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        boolean isUpdate = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);
        Long id = roomSubmitVo.getId();
        if (isUpdate) {
//            删除原有数据
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
//            删除配套相关信息
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
//            删除标签相关信息
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
//            删除支付方式相关信息
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
//            删除租期相关信息
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }

        if(roomSubmitVo.getGraphVoList()!=null){
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : roomSubmitVo.getGraphVoList()){
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(id);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        if (roomSubmitVo.getAttrValueIds()!=null){
            List<RoomAttrValue>  roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : roomSubmitVo.getAttrValueIds()){
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(id).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        if (roomSubmitVo.getFacilityInfoIds() != null){
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : roomSubmitVo.getFacilityInfoIds()){
                RoomFacility roomFacility = RoomFacility.builder().roomId(id).facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        if(roomSubmitVo.getLabelInfoIds() != null){
            List<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : roomSubmitVo.getLabelInfoIds()){
                RoomLabel roomLabel = RoomLabel.builder().roomId(id).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        if (roomSubmitVo.getPaymentTypeIds() != null){
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : roomSubmitVo.getPaymentTypeIds()) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(id).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        if (roomSubmitVo.getLeaseTermIds()!=null){
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
            for (Long leaseTermId : roomSubmitVo.getLeaseTermIds()) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(id).leaseTermId(leaseTermId).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }
    }
}




